<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业" prop="enterpriseId">
        <el-select
          size="small"
          v-model="queryParams.enterpriseId"
          filterable
          placeholder="请选择企业"
          clearable
        >
          <el-option
            v-for="item in zsCustomerList"
            :key="item.id"
            :label="item.enterpriseName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否生效" prop="effect">
        <el-select v-model="queryParams.effect"
                   clearable
                   placeholder="请选择状态">
          <el-option label="未生效" value="0"/>
          <el-option label="已生效" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item label="计费模式" prop="calculationMode">
        <el-select v-model="queryParams.calculationMode"
                   clearable
                   placeholder="请选择状态">
          <el-option label="手动计费" value="手动计费"/>
          <el-option label="自动计费" value="自动计费"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['life:feFacility:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['life:feFacility:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['life:feFacility:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['life:feFacility:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feFacilityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="企业名称" align="center" width="240" prop="enterpriseName"/>
      <el-table-column label="收费开始时间" align="center" prop="rentBeginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rentBeginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收费结束时间" align="center" prop="rentEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rentEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="免收开始时间" align="center" prop="rentFreeBeginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rentFreeBeginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="免收结束时间" align="center" prop="rentFreeEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rentFreeEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="收费模式"
        align="center"
        width="120"
        prop="facilityModel"
      />

      <el-table-column label="下一期账单时间" align="center" prop="nextRentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nextRentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="费用计算方式"
        align="center"
        width="120"
        prop="calculationMode"
      />
      <el-table-column label="计费标准" align="center" prop="price"/>
      <el-table-column
        label="是否生效"
        align="center"
        prop="recordState"
        width="80"
      >
        <template slot-scope="scope">
          <span>{{
              scope.row.effect == '0'
                ? '未生效'
                : scope.row.effect == '1'
                ? '已生效'
                : '暂无'
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240">


        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['life:feFacility:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['life:feFacility:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleBill(scope.row)"
            v-hasPermi="['life:feFacility:edit']"
          >生成账单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="billOpen" width="500px" append-to-body>
      <el-form
        ref="form"
        size="small"
        :model="form"
        :rules="rules"
        label-width="120px">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input disabled v-model="form.enterpriseName"/>
        </el-form-item>

        <el-form-item label="账单类型" prop="billType">
          <el-input disabled v-model="form.billType"/>
        </el-form-item>

        <el-form-item label="账单区间段" prop="billDate">
          <el-date-picker
            v-model="form.billDate"
            size="small"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="下期账单时间" prop="billNextDay">
          <el-date-picker
            clearable
            size="small"
            v-model="form.billNextDay"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择下期账单时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="违约计算时间" prop="billDeadline">
          <el-date-picker
            clearable
            size="small"
            v-model="form.billDeadline"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择违约金收取时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="应收金额" prop="billMoney">
          <el-input v-model="form.billMoney"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitBill">确 定</el-button>
        <el-button @click="billCancel">取 消</el-button>
      </div>
    </el-dialog>





    <!-- 添加或修改设施费用对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="收费开始时间" prop="rentBeginDate">
          <el-date-picker clearable size="small"
                          v-model="form.rentBeginDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择收费开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收费结束时间" prop="rentEndDate">
          <el-date-picker clearable size="small"
                          v-model="form.rentEndDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择收费结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="免收开始时间" prop="rentFreeBeginDate">
          <el-date-picker clearable size="small"
                          v-model="form.rentFreeBeginDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择免收开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="免收结束时间" prop="rentFreeEndDate">
          <el-date-picker clearable size="small"
                          v-model="form.rentFreeEndDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择免收结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="当前费用单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入费用单价"/>
        </el-form-item>

        <el-form-item label="设施费收取模式" prop="facilityModel">
          <el-select
            clearable
            v-model="form.facilityModel"
            placeholder="请选择设施费收取模式">
            <el-option label="以单价按月收费" value="以单价按月收费"></el-option>
            <el-option label="以单价按季度收费" value="以单价按季度收费"></el-option>
            <el-option label="按月固定收费" value="按月固定收费"></el-option>
            <el-option label="按季度固定收费" value="按季度固定收费"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="第一年费用" prop="firstYearFacilityFee">
          <el-input v-model="form.firstYearFacilityFee" placeholder="请输入第一年配套设施使用费(元/平方/月)"/>
        </el-form-item>
        <el-form-item label="第二年费用" prop="secondYearFacilityFee">
          <el-input v-model="form.secondYearFacilityFee" placeholder="请输入第二年配套设施使用费(元/平方/月)"/>
        </el-form-item>
        <el-form-item label="第三年费用" prop="thirdYearFacilityFee">
          <el-input v-model="form.thirdYearFacilityFee" placeholder="请输入第三年配套设施使用费(元/平方/月)"/>
        </el-form-item>
        <el-form-item label="第四年费用" prop="fourthYearFacilityFee">
          <el-input v-model="form.fourthYearFacilityFee" placeholder="请输入第四年配套设施使用费(元/平方/月)"/>
        </el-form-item>
        <el-form-item label="第五年费用" prop="fifthYearFacilityFee">
          <el-input v-model="form.fifthYearFacilityFee" placeholder="请输入第五年配套设施使用费(元/平方/月)"/>
        </el-form-item>
        <el-form-item label="下一期账单时间" prop="nextRentDate">
          <el-date-picker clearable size="small"
                          v-model="form.nextRentDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择下一期账单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="计费模式" prop="calculationMode">
          <el-select v-model="form.calculationMode"
                     clearable
                     placeholder="请选择状态">
            <el-option label="手动计费" value="手动计费"/>
            <el-option label="自动计费" value="自动计费"/>
          </el-select>
        </el-form-item>
        <el-form-item label="是否生效" prop="effect">
          <el-select v-model="form.effect"
                     clearable
                     placeholder="请选择状态">
            <el-option label="未生效" value="0"/>
            <el-option label="已生效" value="1"/>
          </el-select>
        </el-form-item>


        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFeFacility,
  getFeFacility,
  delFeFacility,
  addFeFacility,
  updateFeFacility
} from '@/api/smartParks/operatingFee/facilities'
import { listQyEnterprise } from '@/api/smartParks/merchants/merchantsCompanySettlein'
import { createBill, getFeLeaseRecord } from '@/api/smartParks/operatingFee/leasing'

export default {
  name: 'FeFacility',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      billOpen: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设施费用表格数据
      feFacilityList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        enterpriseId:null,
        pageNum: 1,
        pageSize: 10,
        effect: null,
        calculationMode: null
      },
      zsCustomerList: [],
      // 表单参数
      form: {
        billType:'设施费用',
        leaseRecordId:'',
      },
      // 表单校验
      rules: {
        price:[{
          required: true, message: '收费单价不能为空', trigger: 'blur'
        }],
        facilityModel:[{
          required: true, message: '收费模式不能为空', trigger: 'blur'
        }],

        firstYearFacilityFee: [
          { required: true, message: '第一年配套设施使用费不能为空', trigger: 'blur' }
        ],
        secondYearFacilityFee: [
          { required: true, message: '第二年配套设施使用费不能为空', trigger: 'blur' }
        ],
        thirdYearFacilityFee: [
          { required: true, message: '第三年配套设施使用费不能为空', trigger: 'blur' }
        ],
        fourthYearFacilityFee: [
          { required: true, message: '第四年配套设施使用费不能为空', trigger: 'blur' }
        ],
        fifthYearFacilityFee: [
          { required: true, message: '第五年配套设施使用费不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.getList()
    listQyEnterprise().then((response) => {
      this.zsCustomerList = response.rows
    })
  },
  methods: {
    billCancel() {
      this.billOpen = false
      this.reset()
    },

    submitBill() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if (null != this.form.billDate && '' != this.form.billDate) {
              this.form.billBeginDate = this.form.billDate[0],
                this.form.billEndDate = this.form.billDate[1]
            }
            var req = {
              id: this.form.leaseRecordId,
              billMoney: this.form.billMoney,
              billBeginDate: this.form.billBeginDate,
              billEndDate: this.form.billEndDate,
              billType: this.form.billType,
              billNextDay: this.form.billNextDay,
              billDeadline: this.form.billDeadline
            }
            createBill(req).then(() => {
              this.msgSuccess('账单已生成')
              this.billOpen = false,
                this.getList()
            })
          }
        }
      })

    },

    handleBill(row) {
      this.reset()
      const id = row.id || this.ids
      if (row.calculationMode == '手动计费'){
        getFeFacility(id).then(response => {
          this.form = response.data
          this.form.billType = '设施费用'
          this.billOpen = true
          this.title = '手动生成账单'
        })
      }else {
        this.msgError('当前账单为自动计费,不能手动生成账单')
      }
    },



    /** 查询设施费用列表 */
    getList() {
      this.loading = true
      listFeFacility(this.queryParams).then(response => {
        this.feFacilityList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        firstYearFacilityFee: null,
        secondYearFacilityFee: null,
        thirdYearFacilityFee: null,
        fourthYearFacilityFee: null,
        fifthYearFacilityFee: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        firstYearTotal: null,
        secondYearTotal: null,
        thirdYearTotal: null,
        fourthYearTotal: null,
        fifthYearTotal: null,
        rentFreeBeginDate: null,
        rentFreeEndDate: null,
        rentBeginDate: null,
        rentEndDate: null,
        effect: null,
        nextRentDate: null,
        calculationMode: null,
        price: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加设施费用'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getFeFacility(id).then(response => {
        this.form = response.data
        this.form.effect = this.form.effect +''
        this.open = true
        this.title = '修改设施费用'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFeFacility(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addFeFacility(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除设施费用编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delFeFacility(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('life/feFacility/export', {
        ...this.queryParams
      }, `life_feFacility.xlsx`)
    }
  }
}
</script>
